package com.shuke.sk_worm.controller;

import com.shuke.sk_worm.consts.AbnormalTypeEnum;
import com.shuke.sk_worm.consts.ShopPlatformEnum;
import com.shuke.sk_worm.domain.SkAccountInfo;
import com.shuke.sk_worm.domain.SkShopCookie;
import com.shuke.sk_worm.domain.SkShopInfo;
import com.shuke.sk_worm.service.SkAccountInfoService;
import com.shuke.sk_worm.service.SkShopCookieService;
import com.shuke.sk_worm.service.SkShopInfoService;
import com.shuke.sk_worm.tool.Worm;
import com.shuke.sk_worm.tool.hiskpi.Impl.DouyinShopImpl;
import com.shuke.sk_worm.vo.ShopInfoListVo;
import org.apache.log4j.Logger;
import org.openqa.selenium.os.WindowsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("data")
public class DataController {

    @Autowired
    private SkShopInfoService skShopInfoService;

    @Autowired
    private SkShopCookieService skShopCookieService;

    @Autowired
    private SkAccountInfoService accountInfoService;

    @Autowired
    private DouyinShopImpl douyinShop;

    @Autowired
    private Worm worm;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${shopUpdate.number}")
    private Integer shopUpdateListNum;

    @Value("${shopUpdate.listName}")
    private String shopUpdateName;

    private static final Logger logger =  Logger.getLogger(DataController.class);

    @GetMapping("updateShopStatus/{shopId}")
    public void updateShopStatus(@PathVariable(value = "shopId") Long shopId){
        SkShopInfo shop = skShopInfoService.getById(shopId);
        if (shop == null){
            return;
        }

        if (shop.getAbnormalType() == AbnormalTypeEnum.ACCOUNT_ABNORMAL){
            //获取店铺的TOKEN
            SkShopCookie skShopCookie = skShopCookieService.getCookieByShopId(shopId);
            if (skShopCookie != null){
                //获取账号信息
                List<SkAccountInfo> accountInfos = accountInfoService.getAccountByShopId(shopId);
                updateShopRealUrlData(shop, skShopCookie.getCookie(), accountInfos);
            }
        }
    }

    @Scheduled(cron = "0 20,30,40,50 6-8,9-22 * * ?")
    @GetMapping("updateShopStatus")
    public void updateShopStatusList(){
        //List<Long> shopIds = new ArrayList<>();
       /* String shopUpdateName = this.shopUpdateName + String.valueOf(ShopPlatformEnum.DOUYIN);

        for(int i = 0; i < shopUpdateListNum; i++){
            if (redisTemplate.opsForList().size(shopUpdateName) > 0){
                Object dd = redisTemplate.opsForList().leftPop(shopUpdateName);
                Long shopId = Long.valueOf(dd.toString());
                shopIds.add(shopId);
            }
        }*/
        /*if (redisTemplate.opsForList().size(shopUpdateName) > 0){
            shopIds = redisTemplate.opsForList().range(shopUpdateName, 0, 9);
        }*/

            //List<ShopInfoListVo> shops = skShopInfoService.listShopByIds(ShopPlatformEnum.DOUYIN, shopIds);

        List<ShopInfoListVo> shops = skShopInfoService.listShopByIsAbnormal(ShopPlatformEnum.DOUYIN, 2);
        logger.info("店铺修改队列: " + shops);
        if (shops != null){
            List<ShopInfoListVo> tokenShop = new ArrayList<>();

            shops.forEach(shop->{
                if (shop.getAbnormalType().equals(AbnormalTypeEnum.ACCOUNT_ABNORMAL)){
                    //获取店铺的TOKEN
                    SkShopCookie skShopCookie = skShopCookieService.getCookieByShopId(shop.getId());
                    if (skShopCookie != null){
                        //获取账号信息
                        Boolean status = updateShopRealUrlData(shop, skShopCookie.getCookie(), shop.getAccountInfos());
                        if (status == false){
                            tokenShop.add(shop);
                        }
                    }
                }

                if (shop.getAbnormalType().equals(AbnormalTypeEnum.LOGIN_ABNORMAL)
                        || shop.getAbnormalType().equals(AbnormalTypeEnum.NO_ABNORMAL)
                        || shop.getAbnormalType().equals(AbnormalTypeEnum.AUTH_ABNORMAL)){
                    tokenShop.add(shop);
                }

            });
            worm.saveToken(tokenShop);
            //WindowsUtils.killByName("chrome.exe");
        }
    }

    private Boolean updateShopRealUrlData(SkShopInfo shop, String cookie, List<SkAccountInfo> shopAccountList){
        if (shop.getPlatform() == ShopPlatformEnum.DOUYIN){
            return douyinShop.updateShopRealUrlData(shop, cookie, shopAccountList);
        }
        return true;
    }
}
